<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Hordesxml extends CI_Model
{
	private	$CI;
	private $xml	= NULL;
	private $ghostxml	= NULL;
	private	$key	= NULL;
	
	private $siteKey = "44a6d28ce190359d8a08bca2153a5bb2";
	
	// $data['owner']['nickname]
	// $data['owner']['avatar']
	// $data['owner']['id']
	// $data['citizens'][int]['id']
	// $data['citizens'][int]['nickname']
	// $data['citizens'][int]['avatar']
	// $data['city']['id']
	// $data['city']['name']
	// $data['bank'][#itemID]['id']
	// $data['bank'][#itemID]['name']
	// $data['bank'][#itemID]['count']
	// $data['bank'][#itemID]['cat']
	// $data['bank'][#itemID]['img']
	// $data['bank'][#itemID]['broken']
	private	$data;
	
	
	private	$root_node 		= "hordes";
	private $header_node	= "headers";
	private $owner_node		= "owner";
	private $citizen_node	= "citizen";
	private $citizens_node	= "citizens";
	private $cadaver_node	= "cadaver";
	private $cadavers_node	= "cadavers";
	private $data_node		= "data";
	private	$city_node		= "city";
	private	$bank_node		= "bank";
	private	$item_node		= "item";
	private $game_node		= "game";
	private	$maps_node		= "maps";
	private	$map_node		= "m";
	
	private $error_node		= "error";
	
	private $owner_path;
	private $error_path;
	private	$citizen_path;
	private	$cadavers_path;
	private $city_path;
	private $bank_path;
	private $game_path;
	private	$map_path;
	
	
	public function __construct()
	{
		$this->CI = get_instance();
		
		$this->owner_path = '/'.$this->root_node.'/'.$this->header_node.'/'.$this->owner_node.'/'.$this->citizen_node;
		$this->game_path = '/'.$this->root_node.'/'.$this->header_node.'/'.$this->game_node;
		
		$this->error_path = '/'.$this->root_node.'/'.$this->error_node;
		
		$this->city_path = '/'.$this->root_node.'/'.$this->data_node.'/'.$this->city_node;
		$this->bank_path = '/'.$this->root_node.'/'.$this->data_node.'/'.$this->bank_node.'/'.$this->item_node;
		$this->citizen_path = '/'.$this->root_node.'/'.$this->data_node.'/'.$this->citizens_node.'/'.$this->citizen_node;
		$this->cadavers_path = '/'.$this->root_node.'/'.$this->data_node.'/'.$this->cadavers_node.'/'.$this->cadaver_node;
		
		$this->maps_path = '/'.$this->root_node.'/'.$this->data_node.'/'.$this->maps_node.'/'.$this->map_node;
	}
	
	public function loadXmlInBdd( $inKey )
	{
		$data = $this->loadXml( $inKey );
		if( !$data )
			return false;
			
		$this->load->model('citizen');
		$this->load->model('city');
		
		$this->city->addCity( $data['city'] );
		
		foreach( $data['citizens'] as $citizen )
		{
			$this->citizen->addCitizen( $citizen, $data['city']['id'] );
		}
		
		return $data;
	}
	
	public function loadXml( $inKey, $inAuth=true )
	{
		$url = "";
		if( $inAuth )
		{
			$url = "http://www.hordes.fr/xml?k=".$inKey.";sk=".$this->siteKey;
			$url = "http://www.hordes.fr/xml?k=c825e1454807c94f80ccf6f3411873d218a1072;sk=44a6d28ce190359d8a08bca2153a5bb2";
		}
		else
		{
			$url = "http://www.hordes.fr/xml?k=".$inKey;
		}
		
		
		if( !$xml = simplexml_load_file( $url ) )
			return false;

		$this->key = $inKey;	
		$this->xml = $xml;
		
		// fetch owner info
		if( $inAuth )
		{	// owner info are only in auth xml
			if( !$owner_nodes = $xml->xpath( $this->owner_path ) )
				return false;
			
			$this->data['owner']['nickname']= (string)$owner_nodes[0]['name'];
			$this->data['owner']['avatar']	= (string)$owner_nodes[0]['avatar'];
			$this->data['owner']['id']		= (int)$owner_nodes[0]['id'];
		}
		
		if( $error_nodes = $xml->xpath( $this->error_path ) )
		{
			$this->data['error'] = $error_nodes[0]['code'];
			return $this->data;
		}
		
		if( !$city_nodes = $xml->xpath( $this->city_path ) )
			return false;
		if( !$game_nodes = $xml->xpath( $this->game_path ) )
			return false;
		
		// fetch city info
		$this->data['city']['id'] = (int)$game_nodes[0]['id'];
		$this->data['city']['name'] = (string)$city_nodes[0]['city'];
		
		
		// fetch bank content
		if( $bank_nodes = $xml->xpath( $this->bank_path ) )
		{
			foreach( $bank_nodes as $bank_node )
			{
				$item_id = (string)$bank_node[0]['id'];
				$this->data['bank'][$item_id]['id'] = (int)$bank_node[0]['id'];
				$this->data['bank'][$item_id]['name'] = (string)$bank_node[0]['name'];
				$this->data['bank'][$item_id]['count'] = (int)$bank_node[0]['count'];
				$this->data['bank'][$item_id]['cat'] = (string)$bank_node[0]['cat'];
				$this->data['bank'][$item_id]['img'] = (string)$bank_node[0]['img'];
				$this->data['bank'][$item_id]['broken'] = (int)$bank_node[0]['broken'];
			}
		}
		
		
		// fetch citizens list
		$i = 0;
		if( $citizen_nodes = $xml->xpath( $this->citizen_path ) )
		{
			foreach( $citizen_nodes as $cit_node )
			{
				$this->data['citizens'][$i]['id'] = (int)$cit_node[0]['id'];
				$this->data['citizens'][$i]['nickname'] = (string)$cit_node[0]['name'];
				$this->data['citizens'][$i]['avatar'] = (string)$cit_node[0]['avatar'];
				$i++;
			}
		}
		
		if( $cadavers_nodes = $xml->xpath( $this->cadavers_path ) )
		{
			foreach( $cadavers_nodes as $cad_node )
			{
				$this->data['citizens'][$i]['id'] = (int)$cad_node[0]['id'];
				$this->data['citizens'][$i]['nickname'] = (string)$cad_node[0]['name'];
				$i++;
			}
		}
			
		return $this->data;
	}
	
	
	public function loadGhostXml( $inKey )
	{
		$url = "http://www.hordes.fr/xml?k=".$inKey.";sk=".$this->siteKey;
		if( !$xml = simplexml_load_file( "http://www.hordes.fr/xml/ghost?k=c825e1454807c94f80ccf6f3411873d218a1072;sk=44a6d28ce190359d8a08bca2153a5bb2" ) )
			return false;

		$this->key = $inKey;	
		$this->ghostxml = $xml;
		
		
		if( !$owner_nodes = $xml->xpath( $this->owner_path ) )
			return false;

		if( $error_nodes = $xml->xpath( $this->error_path ) )
		{
			$this->data['error'] = $error_nodes[0]['code'];
			return false;
		}
		
		// fetch maps list
		$i = 0;
		$oldmap = array();
		$oldmap['id'] = 0;
		if( $map_nodes = $xml->xpath( $this->maps_path ) )
		{
			foreach( $map_nodes as $map_node )
			{
				$oldid = (int)$map_node[0]['id'];
				if( $oldid > $oldmap['id'] )
				{
					$oldmap['id'] = $oldid;
					$oldmap['name'] = (string)$map_node[0]['name'];
				}
				$i++;
			}
		}
			
		return $oldmap;
	}
	
}

/* End of file layout.php */
/* Location: ./application/libraries/layout.php */ ?>